//==============================================================================
//
// Title:       RFTestLoopAttenuator.c
// Purpose:     A short description of the implementation.
//
// Created on:  2012-5-8 at 16:28:56 by luh.
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================


#include "RFBB.h"         
#include <utility.h>
#include <formatio.h>	  
#include "hpesg.h"
#include "SetSignalGenerator.h"

//FMON_ACTION_SYNTHESIZER                              
_SHMON_RX_CHAIN_GAIN_CMD g_szShmonRFTestLoopAttCmd[4] ={
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\RF_Test_Loop_Attenuator_Pipe1.txt" ,"RF_Test_Loop_Attenuator_Pipe1.txt",0.0,0.0},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\RF_Test_Loop_Attenuator_Pipe2.txt" ,"RF_Test_Loop_Attenuator_Pipe2.txt",0.0,0.0},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\RF_Test_Loop_Attenuator_Pipe3.txt" ,"RF_Test_Loop_Attenuator_Pipe3.txt",0.0,0.0},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\RF_Test_Loop_Attenuator_Pipe4.txt" ,"RF_Test_Loop_Attenuator_Pipe4.txt",0.0,0.0}};

/**************************************************************************************************
  Function:        RFTestLoopAttenuator
  Description:     This test is the tests the lock detect status of all synthesizers.
  Calls:           none
  Called By:       dll
  Table Accessed:  none
  Table Updated:   none
  Input:           none
  Output:          error code
  Return:          none
  Test Case ID:    FAHA_DigTRX_BIST_SYNTH
  Used Command:    g_szCmonSynOfLock
**************************************************************************************************/
void __decldllexp TX_TEST RFTestLoopAttenuator(tTestData *testData, tTestError *testError)
{

    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    TSObj_Property stepNumericArrayHandle = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};   
    char szElementPath[BUFFER_SIZE_256] ={'\0'};
    
    double dTestNum = 0.0;
    double dFrequency = 0.0;
    double dRxGain = 0.0;
    int  error = 0;
    int  iPassFail = 0;
    int i = 0;
    int iPipeNum = 0;
    int iReturn = 0;
    
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));
    
    TS_PropertyGetPropertyObject(testData->seqContextCVI, &errorInfo,"Step.NumericArray", 0, 
                                        &stepNumericArrayHandle );

    //--------------------------------------------------------------------
    // Get Test Parameters from Sequence FileGlobals    
    sprintf(szElementPath, "%s.%s.PARAMETERS.TestNum", TESTPLAN_BASE_PATH, pszStepName );   
    tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szElementPath, 0, &dTestNum));
    sprintf(szElementPath, "%s.%s.PARAMETERS.TestFrequency", TESTPLAN_BASE_PATH, pszStepName );   
    tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szElementPath, 0, &dFrequency));

    if((dTestNum >= 0) && (dTestNum < 4))
    {
        i = dTestNum;
    }

     //Set signal generation
    iReturn =SetupEsgSignalBbgDLWcdma(0); 
    hpESG_SetFrequencyCW (g_hSignalGenerator, (dFrequency * 1E6));
    hpESG_SetPower( g_hSignalGenerator, 0.0, hpESG_UNIT_DBM );   
    hpESG_SetPowerOn (g_hSignalGenerator, VI_TRUE);
    //--------------------------------------------------------------------
    iReturn = RfbbInetFTPAutoSend(g_szDUTIP, "", "", g_szShmonRFTestLoopAttCmd[i].cLocalFilePath, g_szShmonRFTestLoopAttCmd[i].cRemoteFileName, INET_FTP_FILE_TYPE_BINARY);    
    if(iReturn < 0)
    {
        sprintf( szLogMsg, STRING_MESSAGE_Ftp_File_Err, g_szDUTIP, InetGetErrorMessage( iReturn ) );
        EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        
        goto Error;
    } 

    g_hShellCommand = RfbbInetTelnetOpen (g_szDUTIP, g_iShellCommandPort, 0); 

    //run script    
    sprintf(kszShmonAgent,". %s",g_szShmonRFTestLoopAttCmd[i].cRemoteFileName);
    Shmon_RunScript(kszShmonAgent,5);

    
    //Delay(5);
    
    //send shell command and compare return value 
    iPipeNum = i;
    sprintf(kszShmonAgent,"fpga -MERA%d -r 0x18006",(iPipeNum + 1));
    ErrChk(ShellCommand_HexNumeric(kszShmonAgent, -1,"Read ",&g_szShmonRFTestLoopAttCmd[i].dGetAdcPow));
    
    sprintf(kszShmonAgent,"fpga -MERA%d -r 0x1A040",(iPipeNum + 1));
    ErrChk(ShellCommand_HexNumeric(kszShmonAgent, -1,"Read ",&g_szShmonRFTestLoopAttCmd[i].dGetCorrectFact));

    dRxGain = 10 * log10((g_szShmonRFTestLoopAttCmd[i].dGetAdcPow+  1) / ((g_szShmonRFTestLoopAttCmd[i].dGetCorrectFact * g_szShmonRFTestLoopAttCmd[i].dGetCorrectFact) / 8)) + 2.6 + 0.3;

    sprintf( szLogMsg, "Topi + Rx Gain is %f" ,dRxGain);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    #if 0
    dRxGain=g_szShmonRFTestLoopAttCmd[i].dGetValue +  1;
    dRxGain = dRxGain / 4718592;
    dRxGain = log10(dRxGain);
    dRxGain=10*dRxGain;
    
    dRxGain = dRxGain  + 2.6 + 50.7; 
    #endif
Error:
    
#if(ON_OFF != SIMU_VERS) 
    hpESG_SetPowerOn (g_hSignalGenerator, VI_FALSE);

    if(iPassFail == 1)
    {
        testData->result = PASS;
    }
    else 
    {
        testData->result = FAIL;
    }
    
    testData->measurement = dRxGain;

    InetTelnetClose(g_hShellCommand); 
    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}


